#version 120

uniform sampler1D tranf;
uniform sampler2D backfacetexture;
uniform sampler2D Image;
uniform sampler3D volumetexture;


uniform float stepsize;
uniform float begin;
uniform float isoValue;

varying vec4 vposition;
varying vec3 eposition;
uniform vec4 lightposition;


const float shininess = 5;
const float diffuse = 0.0;
const float specular = 1.0;


vec4 DirectVolumeRendering(vec3 front, vec3 back, float upperThreshold, float threshold, float offset)
{
	vec3 dir;
	dir = back - front;
	float len = length(dir);
	vec3 norm_dir = normalize(dir);
	//float iso_value = 0.1;

	vec3 rayIncrement = stepsize*norm_dir;

	vec3 pos = front;
	vec4 value=vec4(0,0,0,0);
	// accumlate value
	vec4 result = vec4(0,0,0,0);//RGBA
	float raylength = 0;

    float scale = 1.0/(upperThreshold - threshold);
    float res = 0;
    
	for(int i=0; i<2000; i++)
	{
		float factor = 0.5;

	
		value = texture3D(volumetexture, pos);
	
		pos += rayIncrement;
		raylength += length(rayIncrement);

			vec4 texValue = texture1D(tranf, value.a);

			result.rgb += (1.0-result.a)*texValue.a*texValue.rgb;
			result.a += (1.0-result.a)*texValue.a;

			if (result.a >= 0.9 || raylength > len ) 
			{
				break;
			}
	}
	return result;

}

 float RGBdistance(vec4 a, vec4 b)
 {
	return sqrt( (a.r-b.r)*(a.r-b.r) +(a.g-b.g)*(a.g-b.g) + (a.b-b.b)*(a.b-b.b));
 }

void main()
{
	vec2 texposition = ((vposition.xy/vposition.w) + 1.0)/2.0;
	//interleave sampling pattern========================================
	//float w=800;
	//float h=800;
	//vec2 interleavePosf = vec2(texposition.x*w, texposition.y*h);
	//ivec2 interleavePos;
	//interleavePos.x = int(floor(interleavePosf.x + 0.5));
	//interleavePos.y = int(floor(interleavePosf.y + 0.5));

	//interleavePos.x = int(mod(interleavePos.x,8));
	//interleavePos.y = int(mod(interleavePos.y,8));
	//float offset = offset(interleavePos);	
	//===================================================================
   vec4 sum = vec4(0.0);
   float onePixel = 1.0/800.0;
   sum = texture2D(Image, vec2(texposition.x, texposition.y)) ;
   vec4 up = texture2D(Image, vec2(texposition.x, texposition.y+onePixel)) ;
   vec4 down = texture2D(Image, vec2(texposition.x, texposition.y-onePixel)) ;
   vec4 right = texture2D(Image, vec2(texposition.x+onePixel, texposition.y)) ;
   vec4 left = texture2D(Image, vec2(texposition.x-onePixel, texposition.y)) ;
 float refine = 0;
 float threshold = 0.03;
	if( RGBdistance(sum,up)<threshold && 
		RGBdistance(sum,down)<threshold &&
		RGBdistance(sum,right)<threshold &&
		RGBdistance(sum,left)<threshold )
	{
		discard;
	}	//gl_FragColor = sum;
		//gl_FragColor = vec4(1,0,0,1);
	//=====================================================
	  //gl_FragColor =  vec4(refine);//texture2D(backfacetexture, texposition);

		vec3 start = gl_TexCoord[0].xyz;
		vec3 backposition = texture2D(backfacetexture, texposition).rgb;

		float upperThreshold = 1.0;
		float thresholdV = 0.1;
		vec4 result = DirectVolumeRendering(start, backposition, upperThreshold, thresholdV, 0);
		gl_FragColor = (1-result.a)*vec4(0.2, 0.2, 0.2, 1) + result.a * result;

}